/* 
  BBE Sonic Maximizer modelisation

  (C) 20011, Dominique Guichaoua.

  Peek Detector code is Copyright (C) 2011 lubomir i. ivanov

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

*/


desc:BBE Sonic Maximizer

slider1:0<0,10,.1>Low Contour (dB)
slider2:0<0,10,.1>Process (dB)
slider3:0<-30,0,.1>Output Level (dB)
slider4:0<0,10,.1>Peek Detector Level

@init
c_ampdB = 8.65617025;

//Electronic Components
R=21500;
C1=0.0000000033;
C2=0.000000033;

Te=1/srate;

//Input Gain
Gin=-2;
//Band Pass Gain
GBP=0.72;
//Low Pass initial Gain (dB)
GLP0=0.47;
//High Pass initial Gain(dB)
GHP0=0.47;

//High Pass Coefficient
a0HP = -Te/(2*R*C1);
b0HP = a0HP;
//Low  Pass Coefficient
a0LP = -Te/(2*R*C2);
b0LP = a0LP;

//Peek Detector Coefficients
//  Copyright (C) 2011 lubomir i. ivanov
a0cv = 0.0003;
b0cv = 1 - a0cv;
//end lubomir i. ivanov code

//Front Panel Controls
@slider
//Low Pass Gain
GLP = GLP0*exp((slider1)/c_ampdB);
//High Pass Gain
GHP = GHP0*exp((slider2+GHP0)/c_ampdB);
//Ouput Gain
amp=exp(slider3/c_ampdB);

//Peek Detector close loop gain
//  Copyright (C) 2011 lubomir i. ivanov
cv_k = slider4;
//end lubomir i. ivanov code

@sample
//Input Buffer
s0=spl0*Gin;
s1=spl1*Gin;

//Peak Detector
//  Copyright (C) 2011 lubomir i. ivanov
cv0 = 1 - abs(s0)*cv_k;
cv1 = 1 - abs(s1)*cv_k;

cv_rc0 = a0cv*cv0 + b0cv*cv_rc0;
cv_rc1 = a0cv*cv1 + b0cv*cv_rc1;
//end lubomir i. ivanov code

//Pseudo State-Variable Filter (Trapezes)
BP0 = a0HP*HP0 + b0HP*HP0tmp + BP0;
BP1 = a0HP*HP1 + b0HP*HP1tmp + BP1;
HP0tmp = HP0;
HP1tmp = HP1;

LP0 = a0LP*BP0 + b0LP*BP0tmp + LP0;
LP1 = a0LP*BP1 + b0LP*BP1tmp + LP1;
BP0tmp = BP0;
BP1tmp = BP1;

HP0 = s0 + 1.5*BP0 - LP0;
HP1 = s1 + 1.5*BP1 - LP1;

//Summing Output Buffer
spl0 = -(LP0 * GLP + BP0 * GBP + HP0 * GHP * cv_rc0)*amp;
spl1 = -(LP1 * GLP + BP1 * GBP + HP1 * GHP * cv_rc1)*amp;